#!/usr/bin/env python3

import pygame
import os
import pdb
import logging
from collections import namedtuple

# 配置log输出的级别和格式
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

class Point:
    def __init__(self, _x, _y):
        self.x = _x
        self.y = _y

    def __repr__(self):
        return f"Point(x={self.x}, y={self.y})"

    def __iter__(self):
        yield self.x
        yield self.y

#p0:起点
#p1:终点
#p2:控制点
#return: 曲线点序列
def quadratic_bezier_curve(p0, p1, p2, point_num):
    step = int(100/point_num)
    bezier_curve = []
    x_list = []
    y_list = []
    for t in range(0, 100 + step, step):
        t = float(t/100)
        pos = Point(0,0)
        pos.x = (1 - t) ** 2 * p0.x + 2 * (1 - t) * t * p1.x + t ** 2 * p2.x
        pos.y = (1 - t) ** 2 * p0.y + 2 * (1 - t) * t * p1.y + t ** 2 * p2.y
        x_list.append(pos.x)
        y_list.append(pos.y)
        bezier_curve.append(pos)
    return bezier_curve, x_list, y_list

def cubic_bezier_curve(p0, p1, p2, p3, point_num):
    step = int(100/point_num)
    bezier_curve = []
    x_list = []
    y_list = []
    for t in range(0, 100 + step, step):
        t = float(t/100)
        pos = Point(0,0)
        pos.x = (1 - t) ** 3 * p0.x + 3 * t * (1 -t) ** 2 * p1.x + 3 * t ** 2 * (1 -t) * p2.x + t ** 3 * p3.x
        pos.y = (1 - t) ** 3 * p0.y + 3 * t * (1 -t) ** 2 * p1.y + 3 * t ** 2 * (1 -t) * p2.y + t ** 3 * p3.y
        bezier_curve.append(pos)
        x_list.append(pos.x)
        y_list.append(pos.y)
    return bezier_curve, x_list, y_list